package ui.swing.model;

import java.math.BigDecimal;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import spring.SpringApplicationCtx;

import com.stock.dao.Buysave;
import com.stock.dao.BuysaveDAO;

import data.Constant;
import data.LoadData;

public class BenifitModel  extends DefaultTableModel implements Stoppable{

	private static final long serialVersionUID = -5558736109408825460L;
	protected String[] col = new String[]{"userName","code","name","buyDateTime","buyPrice","curPrice","salePrice","ratePercent"};
	private boolean run = true;
	
	protected Object[][] datas ;

	public BenifitModel() {
		BuysaveDAO bDao = (BuysaveDAO) SpringApplicationCtx.getCtx().getBean("BuysaveDAO");
		List<Buysave> bss = bDao.findAll();
		datas = new Object[bss.size()][8];
		int i = 0;
		for (Buysave buysave : bss) {
			BigDecimal curPice = LoadData.getTimeStock(buysave.getNumber()).getCurPrice();
			datas[i][0] = buysave.getUserName();
			datas[i][1] = buysave.getNumber();
			datas[i][2] = LoadData.getTimeStock(buysave.getNumber()).getStockName();
			datas[i][3] = buysave.getBuyDate().toString()+" "+buysave.getBuyTime();
			datas[i][4] = buysave.getBuyPrice().toString();
			datas[i][5] = curPice.toString();
			datas[i][6] = buysave.getSalePrice()==null?0:buysave.getSalePrice();
			if(buysave.getSalePrice()==null||buysave.getSalePrice().compareTo(BigDecimal.ZERO)==0){
				BigDecimal benifitPercent = curPice.subtract(buysave.getBuyPrice()).multiply(Constant.ONEHUNDRED)
						.divide(buysave.getBuyPrice(),2,BigDecimal.ROUND_HALF_UP);
				datas[i][7] = benifitPercent.toString();
				if(benifitPercent.compareTo(BigDecimal.ZERO)>0){
					datas[i][7] = "+"+benifitPercent.toString();
				}
			}else{
				BigDecimal benifitPercent = buysave.getSalePrice().subtract(buysave.getBuyPrice()).multiply(Constant.ONEHUNDRED)
						.divide(buysave.getBuyPrice(),2,BigDecimal.ROUND_HALF_UP);
				datas[i][7] = benifitPercent.toString();
				if(benifitPercent.compareTo(BigDecimal.ZERO)>0){
					datas[i][7] = "+"+benifitPercent.toString();
				}
			}
			i++;
		}
		setDataVector(datas,col);
		
		Thread th = new Thread(this);
		th.start();
	}
	
	@Override
	public void run() {
		while(run){
			try {
				loadData();
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}

	private void loadData() {
		for (int i = 0; i < datas.length; i++) {
			BigDecimal curPice = LoadData.getTimeStock(datas[i][1].toString()).getCurPrice();
			setValueAt(curPice, i, 5);
			if(datas[i][6].toString().equals("0")){
				BigDecimal benifitPercent = curPice.subtract(new BigDecimal(datas[i][4].toString())).multiply(Constant.ONEHUNDRED)
						.divide(new BigDecimal(datas[i][4].toString()),2,BigDecimal.ROUND_HALF_UP);
				if(benifitPercent.compareTo(BigDecimal.ZERO)>0){
					datas[i][7] = "+"+benifitPercent.toString();
				}
				setValueAt(datas[i][7], i, 7);
			}
		}
	}

	@Override
	public void stop() {
		run = false;
	}

}
